package bin_tree.heap;


import java.util.Comparator;
import java.util.Queue;
import java.util.PriorityQueue;

import java.util.Arrays;

/**
 * @author yuisama
 * @date 2022/03/02 19:24
 **/
public class PriorityQueueTest {
    public static void main(String[] args) {
        // 通过构造方法传入比较器
        // 默认是最小堆，"值"(比较器compare的返回值)越小，优先级就越高
        // 当传入一个降序的比较器时,值(比较器compare的返回值，值越大，返回负数)
//        Queue<Student> queue = new PriorityQueue<>(new StudentComDesc());
//        Queue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o2.getAge() - o1.getAge();
//            }
//        });
        Queue<Student> queue = new PriorityQueue<>((o1,o2) -> o2.getAge() - o1.getAge());
        Student stu1 = new Student("铭哥",40);
        Student stu2 = new Student("龙哥",20);
        Student stu3 = new Student("蛋哥",18);
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

class StudentCom implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

class StudentComDesc implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

class Student {
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}